package arithmetic1.tiantisai;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Demo05 {
    public static void main(String[] args) throws IOException {
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        int num=Integer.parseInt(reader.readLine());
        String arr[][]=new String[3][num];
        for(int i=0;i<num;i++){
            String str=reader.readLine();
            String[] strs = str.split("[ ]");
            arr[0][i]=strs[0];
            arr[1][i]=strs[1];
            arr[2][i]=strs[2];
        }
        int count=Integer.parseInt(reader.readLine());
        String str=reader.readLine();
        String[] strs = str.split("[ ]");
        for(String code:strs){
            int i=function(arr,code);
            System.out.println(arr[0][i]+" "+arr[2][i]);
        }
    }

    public static int function(String arr[][],String code){
        for (int i = 0; i <arr[0].length ; i++) {
            if(arr[1][i].equals(code)){
                return i;
            }
        }
        return 0;
    }
}
